<template>
	<view>
		<image src="../../static/logo-1.png" class="logo" mode="widthFix"></image>
		<input class="uni-input" v-model="realName" focus placeholder="请输入真实姓名" />
		

		
		<!-- <input class="uni-input" v-model="workRegion" type="text" placeholder="请输入所属工区" /> -->
		
		
		<!-- <input class="uni-input" v-model="department" type="text" placeholder="请输入所属部门" /> -->
		
		<!-- 公司 -->
		<view class="uni-list">
		<picker @change="bindPickerChange3" :value="index3" :range="companys" >
			<view class="uni-input" :class="index3 == 0? 'uni-input':''">{{companys[index3]}}</view>
		</picker>
		</view>
		
		<!-- 部门 -->
		<view class="uni-list">
		<picker @change="bindPickerChange2" :value="index2" :range="allDepartment" :range-key="'departmentName'">
			<view class="uni-input" :class="index2 == 0? 'uni-input':''">{{allDepartment[index2].departmentName}}</view>
		</picker>
		</view>
		
		<!-- 工区 -->
		<view class="uni-list" v-if="IfWorkRegion">
		<picker @change="bindPickerChange" :value="index" :range="allWorkRegion" :range-key="'regionName'">
			<view class="uni-input" :class="index == 0? 'uni-input':''">{{allWorkRegion[index].regionName}}</view>
		</picker>
		</view>
		<button class="registration-btn" @tap="toRegistration">注册</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 姓名,手机号,部门,工区
				realName:"",
				phoneNumber:"",
				department:"",
				workRegion:"",
				index:0,
				index2:0,
				allWorkRegion:[{regionName:"请选择工区"}],
				allDepartment:[{departmentName:"请选择部门"}],
				IfWorkRegion:false,
				
				companys:["请选择公司","广州市佑安土木工程有限公司", "汕尾市佑安沥青技术有限公司"],
				index3:0,
			};
		},
		onLoad(options){
			console.log(options)
			this.phoneNumber = options.value
			// this.findAllRegioWork();
			this.findAllDepartment();
		},
		methods:{
			// 注册
			toRegistration:function(){
				let that=this
				let data = {}
				if(that.realName.length == 0 || that.phoneNumber.length == 0 || that.index2 == 0 || (that.IfWorkRegion && that.index == 0) || that.index3 == 0){
					uni.showToast({
						icon: "error",
						title: "请输入完整信息"
					})
					return
				}
				data.realName = that.realName
				data.department = that.allDepartment[that.index2].id
				if(that.IfWorkRegion){
					data.workRegion = that.allWorkRegion[that.index].id
				}
				data.phoneNumber = that.phoneNumber
				data.company = that.companys[that.index3]
				
				that.ajax(that.url.registration,"POST",data,function(resp){
					console.log(resp)
					// 注册成功，跳转
					if(resp.data.code === 200){
						uni.setStorageSync("Token", resp.data.data)
						uni.redirectTo({
							url:"../checkin_noface2/checkin_noface2"
						})
					}
				})
			},
			findAllRegioWork: function(){
				let that=this
				let data = {}
				data.departmentId = that.allDepartment[that.index2].id
				that.index == 0
				that.ajax(that.url.findAllRegioWork+"/"+data.departmentId,"GET",null,function(resp){
					console.log(resp.data.data)
					let i = 0;
					if(resp.data.data.length == 0){
						that.IfWorkRegion = false
						return
					}else{
						for(i = 0; i < resp.data.data.length; i++){
							console.log(resp.data.data[i])
							that.allWorkRegion.push(resp.data.data[i])
						}
						that.IfWorkRegion = true
					}

					// that.allWorkRegion = resp.data.data
				})
			},
			findAllDepartment: function(){
				console.log("findAllDepartment")
				let that=this
				that.ajax(that.url.findAllDepartment,"GET",null,function(resp){
					console.log(resp.data.data)
					let i = 0;
					for(i = 0; i < resp.data.data.length; i++){
						console.log(resp.data.data[i])
						that.allDepartment.push(resp.data.data[i])
					}
				})
			},
			bindPickerChange: function(e){
				this.index = e.detail.value
			},
			bindPickerChange2: function(e){
				this.index2 = e.detail.value;
				this.findAllRegioWork()
			},
			bindPickerChange3: function(e){
				this.index3 = e.detail.value;
			},
			
		}
	}
</script>

<style lang="less">
	@import url("registration.less");

</style>
